// pages/renzhen/edu/edu.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.globalData.imgurl,
    date: '2016-09-01',
    startdate: '选择日期',
    enddate: '选择日期',
    updateList: {
      card: {
        showVideo: false,
      },
      idcard: {
        showVideo: false,
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDate()
  },
  // 获取学校名称
  getname: function (e) {
    this.setData({
      corpname: e.detail.value
    })
  },
  // 获取专业名称
  geteduname: function (e) {
    this.setData({
      jobname: e.detail.value
    })
  },
  // 获取今天
  getDate() {
    var _date = new Date();
    var endDate = _date.getFullYear() + '-' + (_date.getMonth() + 1) + '-' + _date.getDate()
    console.log(endDate)
    this.setData({
      endDate: endDate
    })
  },
  // 选择开始时间
  bindStartDate: function (e) {
    this.setData({
      startdate: e.detail.value
    })
  },
  // 选择结束时间
  bindEndDate: function (e) {
    this.setData({
      enddate: e.detail.value
    })
  },
  // 上传
  update: function (e) {
    var that = this
    var o = e.currentTarget.dataset;
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      compressed: 'true',
      maxDuration: 10,
      success: function (res) {
        console.log(res)
        var videoUrl = res.tempFilePath
          that.upload(o,videoUrl)
        
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 上传
  upload: function (o, videoUrl) {
    var that = this
    console.log(videoUrl)
    var updateList = that.data.updateList
    wx.uploadFile({
      url: app.globalData.url + 'Organize/uploadvideo',
      filePath: videoUrl,
      name: 'video',
      success: function (res) {
        if (!res.data.code) {
          var data = JSON.parse(res.data)
          console.log(data)
          if (data.code == 1) {
            updateList[o.uptype].showVideo = true
            updateList[o.uptype].videoUrl = data.path
            that.setData({
              updateList: updateList
            })
          } else {

          }
        }


      }
    })
  },
  // 提交
  summit: function () {
    var that = this
    var data = {
      wxopenid: wx.getStorageSync('openId'),
      edu_name: this.data.corpname,
      edu_major: this.data.jobname,
      edu_startime: this.data.startdate,
      edu_endtime: this.data.enddate,
      edu_license: this.data.updateList.card,
      identity_card: this.data.updateList.idcard,
      organize_id: this.data.selectedOrg[0].id
    }
    if (!data.edu_name || data.edu_name == '') {
      that.showmodal('请填写学校名称')
    } else if (!data.edu_startime || data.edu_startime == '') {
      that.showmodal('请选择上学时间')
    } else if (!data.edu_endtime || data.edu_endtime == '') {
      that.showmodal('请选择毕业时间')
    // } else if (!data.edu_license || data.edu_license == '') {
    //   that.showmodal('请添加名片或工牌')
    // } else if (!data.identity_card || data.identity_card == '') {
    //   that.showmodal('请添加身份证')
    } else {
      wx.request({
        url: app.globalData.url+'Card/addedu',
        data: data,
        success: function (res) { 
          console.log(res)
          wx.navigateBack({
            delta: 1,
          })
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  // 提示弹窗
  showmodal: function (e) {
    wx.showModal({
      title: '提示',
      content: e,
      confirmColor: '#ff8e03',
      showCancel: true,

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/organizes',
      success: function (res) {
        console.log(res)
        var organizes = res.data.organizes
        that.setData({
          organizes: organizes
        })
      }
    })
  },
  //显示市
  showOrg: function (e) {
    var o = e.currentTarget.dataset
    this.setData({
      showOrg: !this.data.showOrg,
    })


  },
  // 选择市
  selectOrg: function (e) {
    var citytype = this.data.citytype
    var o = e.currentTarget.dataset
    var organizes = this.data.organizes
    for (var i = 0; i < organizes.length; i++) {
      organizes[i].selected = false;
    }
    organizes[o.index].selected = !organizes[o.index].selected
    var selectedOrg = []
    for (var i = 0; i < organizes.length; i++) {
      if (organizes[i].selected) {
        organizes[i].date = app.getLocalTime(Math.round(new Date().getTime() / 1000), 10)
        selectedOrg.push(organizes[i])
      }
    }
    this.setData({
      orgIndex: o.index,
      organizes: organizes,
      selectedOrg: selectedOrg
    })

  },
  close: function () {
    this.setData({
      showOrg: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})